const { Router } = require('express')
const express = require('express')
//路由容器
const whoareyou = express.Router()
const axios = require('axios')
const multipart = require('connect-multiparty');
const FormData = require('form-data')
var multipartMiddleware = multipart()
const fs = require("fs");
const { JsonWebTokenError } = require('jsonwebtoken')
const jwt = require("jsonwebtoken")
whoareyou.get('/realName/queryRealNameByUserId', (req, res) => {
    let {userId}=req.query
    axios({
        url: '/realName/queryRealNameByUserId',
        method: 'get',
        params: {
            userId:userId
        }
    }).then(function(data){
        res.status(200).json({
            data:data.data,
            code:0
        })
    }).catch(function(err){
        res.status(500).json({
            msg:err
        })
    })
})
whoareyou.get('/realName/getNation', (req, res) => {
    // console.log(req.query)
    axios({
        url: '/realName/getNation',
        method: 'get',
        params: {
        }
    }).then(function(data){
        res.status(200).json({
            data:data.data,
            code:0
        })
    }).catch(function(err){
        res.status(500).json({
            msg:err
        })
    })
})
whoareyou.get('/realName/getProvince', (req, res) => {
    // console.log(req.query)
    axios({
        url: '/realName/getProvince',
        method: 'get',
        params: {
        }
    }).then(function(data){
        res.status(200).json({
            data:data.data,
            code:0
        })
    }).catch(function(err){
        res.status(500).json({
            msg:err
        })
    })
})
whoareyou.post('/realName/addRealName', (req, res) => {
    let { email,idCard,idType,identity,nation,nativePlace,realName,telephone,birth,sex}=req.body.params
    axios({
        method:'post',
        url : '/realName/addRealName',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            token:req.session.token
        },
        params:{
            email:email,
            idCard:idCard,
            idType:idType,
            identity:identity,
            nation:nation,
            nativePlace:nativePlace,
            realName:realName,
            telephone:telephone,
            sex:sex,
            birth:birth,
            userId:jwt.decode(req.session.token).id
     }
    }).then(function(data){
        res.status(200).json({
            data:data.data,
            code:0
        })
    }).catch(function(err){
        res.status(500).json({
            msg:'失败'
        })
    })
 })
 whoareyou.put('/realName/updateRealName',multipartMiddleware,  (req, res) => {
     console.log(req.query)
     console.log(req.files)
        axios({
            method:"put",
            url:`http://www.mianded.top:8081/realName/updateRealName?birth=${encodeURI(
                     40
                 )}&email=${encodeURI(
                    req.query.email
                )}&idCard=${encodeURI(
                    req.query.idCard
                )}&identity=${encodeURI(
                    req.query.identity
                )}&nation=${encodeURI(
                    req.query.nation
                )}&nativePlace=${encodeURI(
                    req.query.nativePlace
                )}&realName=${encodeURI(
                    req.query.realName
                )}&realNameId=${encodeURI(
                    4
                )}&sex=${encodeURI(
                    req.query.sex
                )}&telephone=${encodeURI(
                    req.query.telephone
                )}`,
            contentType:"application/json",
            maxContentLength:Infinity,
            maxRedirects:5,
            maxBodyLength:Infinity,
        })
        .then(function(data){
            console.log("success============")
            console.log(data.data)
            res.send({data: data.data})
        })
        .catch((error) => {
            console.log(error.response);
            console.log(error.response.status);
            res.send(error)
        })
 })
whoareyou.put('/realName/uploadCertificatesImg',multipartMiddleware, (req, res) => {
        let file = req.files.multipartFile;
        let userId = req.query.userId;
        const uri = file.path
        let multipartFile = new FormData();
        multipartFile.append('img', fs.createReadStream(uri));
        axios.put(`/realName/uploadCertificatesImg?userId=${userId}`, multipartFile, { headers: multipartFile.getHeaders() }).then((data) => {
            console.log("success============")
            console.log(data.data)
            res.send({ data: data.data })
        }).catch((err) => {
            console.log("err");
            res.send(err)
        })
})
whoareyou.put('/realName/uploadIdImgPositive',multipartMiddleware, (req, res) => {
    let file = req.files.multipartFile;
    let userId = req.query.userId;
    const uri = file.path
    let multipartFile = new FormData();
    multipartFile.append('img', fs.createReadStream(uri));
    axios.put(`/realName/uploadIdImgPositive?userId=${userId}`, multipartFile, { headers: multipartFile.getHeaders() }).then((data) => {
        console.log("success============")
        console.log(data.data)
        res.send({ data: data.data })
    }).catch((err) => {
        console.log("err");
        res.send(err)
    })
})
whoareyou.put('/realName/uploadIdImgFace',multipartMiddleware, (req, res) => {
    let file = req.files.multipartFile;
    let userId = req.query.userId;
    const uri = file.path
    let multipartFile = new FormData();
    multipartFile.append('img', fs.createReadStream(uri));
    axios.put(`/realName/uploadIdImgFace?userId=${userId}`, multipartFile, { headers: multipartFile.getHeaders() }).then((data) => {
        console.log("success============")
        console.log(data.data)
        res.send({ data: data.data })
    }).catch((err) => {
        console.log("err");
        res.send(err)
    })
})

module.exports=whoareyou;